const app = getApp()
const util = require('../../../utils/util.js')

// 返回处理
const handleRes = (response, fn, url, data, confirmFn) => {
    console.log(url, 'res-->>', response)
    if (response.statusCode == 200) {
        if (response.data.result == 100000) {
            wx.reLaunch({
                url: '../index/index',
            })
            return
        } else if ((response.data.result == 0 || response.data.result === undefined) && typeof fn == 'function') {
            fn(response.data)
            return;
        } else if (response.data.result == 1001) {
            wx.showModal({
                title: '温馨提示',
                confirmColor: '#8290DD',
                content: response.data.description,
                success(res) {
                    if (res.confirm) {
                        console.log('用户点击确定');
                        if (confirmFn) {
                            confirmFn(true);
                        }
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                    }
                }
            })
            return;
        } else {
            wx.showModal({
                title: '温馨提示',
                confirmColor: '#8290DD',
                showCancel: false,
                content: response.data.description
            })
        }
    } else {
        // app.sendLog(url, JSON.stringify(response), JSON.stringify(data));
        wx.showToast({
            title: '服务' + response.statusCode,
            image: '/images/gantan.png',
            duration: 2500
        })
        return;
    }
}

const handleErr = (error, url, data) => {
    console.log(url, 'error-->>', error)
    wx.showToast({
        title: '请求失败请重试',
        image: '/images/gantan.png',
        duration: 2500
    })
}

const handleRequest = (data, fn, url, confirmFn) => {

    handleRequestCallback(data, fn, url, confirmFn)

}

const handleRequestCallback = (data, fn, url, confirmFn) => {
    wx.showLoading({
        title: '加载中...',
        mask: true
    })

    wx.request({
        url: util.CONST_DATA.BASE_URL + url,
        header: header,
        method: method,
        data: data,
        success: function (response) {
            wx.hideLoading()
            handleRes(response, fn, url, data, confirmFn)
        },
        fail: function (error) {
            wx.hideLoading()
            handleErr(error, url, data)
        }
    })
}

const header = app.globalData.header
const method = 'POST'

const lbbNfcService = {
    /**
     * 获取rfid编号
     */
    getRfids: (data, fn) => {
        let url = 'nfc/getRfids'
        handleRequest(data, fn, url)
    },
    logWriteRfid: (data, fn) => {
        let url = 'nfc/log/writeRfid'
        handleRequest(data, fn, url)
    },
    logReadRfid:(data, fn) => {
        let url = 'nfc/log/readRfid'
        handleRequest(data, fn, url)
    },
    getEquNumByRfid:(data, fn) =>{
        let url = 'nfc/getEquNumByRfid'
        handleRequest(data, fn, url)
    }

}

module.exports = lbbNfcService